package mcom.xmks.bluetooth.mesh.storage;

import com.tencent.mmkv.MMKV;
import mcom.xmks.bluetooth.mesh.utils.LogTool;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 存储工具
 */
public class StorageUtil {
    private static final String IVINDEX="iv_index";
    private static final String FLAG="flag";
    private static final String NODE_CONFIG_KEY="config_key";
    private static final String MMKV_NODE="mmkv_node";
    private static final String MMKV_OTHER="mmkv_other";

    public static void putNode(NodeItem nodeItem){
        MMKV defaultMMKV = MMKV.mmkvWithID( MMKV_NODE);
        defaultMMKV.encode(createNodeAddress(nodeItem.getNodeAddress()),nodeItem);
    }
    public static NodeItem getNode(int nodeAddress){
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_NODE);
        return defaultMMKV.decodeParcelable(createNodeAddress(nodeAddress),NodeItem.class);
    }
    public static NodeItem getNode(String nodeAddress){
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_NODE);
        return defaultMMKV.decodeParcelable(nodeAddress, NodeItem.class);
    }

    /**
     * 删除节点（本地数据库存储）
     * @param nodeAddress 节点地址
     */
    public static void  deleteNode(int nodeAddress){
        LogTool.d(String.format("delete node 0x${}",Integer.toHexString(nodeAddress)));
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_NODE);
        defaultMMKV.remove(createNodeAddress(nodeAddress));
    }

    /**
     * 跟新节点状态
     * @param nodeAddress 节点地址
     * @param state 状态
     */
    public static void updateNodeState(int nodeAddress,boolean state){
        NodeItem node = getNode(nodeAddress);
        if(node!=null){
            putNode(new NodeItem(node.getName(),node.getNodeAddress(),state));
        }
    }
    /**
     * 更新节点名称
     * @param nodeAddress
     * @param name
     */
    public static void updateNodeName(int nodeAddress,String name){
        NodeItem node = getNode(nodeAddress);
        if(node!=null){
            putNode(new NodeItem(name,node.getNodeAddress(),node.isState()));
        }
    }

    /**
     * 检查节点是否已经存在
     * @param nodeAddress
     * @return
     */
    public static boolean checkNodeHasExist(int nodeAddress){
       List<NodeItem> allNode = getAllNode();
        for (NodeItem nodeItem : allNode){
            if(nodeItem.getNodeAddress()==nodeAddress){
                return true;
            }
        }
        return false;
    }
    /**
     * 获取所有已注册节点
     * @return
     */
    public static List<NodeItem> getAllNode(){
        MMKV defaultMMKV = MMKV.mmkvWithID(MMKV_NODE);
        String[] allKeys = defaultMMKV.allKeys();
        List<NodeItem> mutableList=new ArrayList<>();
        if(allKeys!=null) {
            for (String key : allKeys) {
                NodeItem item = getNode(parseNodeAddress(key));
                if (item != null) {
                    mutableList.add(item);
                }
            }
        }
        return mutableList;
    }

    /**
     * 获取存储的设备IV Index
     * @return
     */
    public static int getIvIndex(){
        return MMKV.mmkvWithID(MMKV_OTHER).decodeInt(IVINDEX,0);
    }

    /**
     * 将设备IV Index写入表单
     * @param ivIndex
     * @return
     */
    public static boolean setIvIndex(int ivIndex){
        LogTool.d(String.format("setIvIndex:${}", ivIndex));
        return MMKV.mmkvWithID(MMKV_OTHER).encode(IVINDEX,ivIndex);
    }
    /**
     * 获取网络密钥刷新标志
     * @return
     */
    public static int getFlag(){
        return MMKV.mmkvWithID(MMKV_OTHER).decodeInt(FLAG,0);
    }
    public static boolean setFlag(int flag){
        LogTool.d(String.format("setFlag:${}", flag));
        return MMKV.mmkvWithID(MMKV_OTHER).encode(FLAG,flag);
    }
    public static String getConfigKey(){
        return MMKV.mmkvWithID(MMKV_OTHER).decodeString(NODE_CONFIG_KEY);
    }
    public static boolean setConfigKey(String key){
        LogTool.d(String.format("setConfigKey:${}", key));
        return MMKV.mmkvWithID(MMKV_OTHER).encode(NODE_CONFIG_KEY,key);
    }

    /**
     * 创建节点地址
     * @param nodeAddress
     * @return
     */
    private static String createNodeAddress(int nodeAddress){
        return String.format(Locale.getDefault(),"%04X",nodeAddress);
    }

    /**
     * 解析节点地址
     * @param nodeAddress
     * @return
     */
    private static int parseNodeAddress(String nodeAddress){
        return Integer.parseInt(nodeAddress,16);
    }
}
